//
//  RoomManagerVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/7.
//  Copyright © 2020 tym. All rights reserved.
//

#import "RoomManagerVC.h"
#import "AddRoomVC.h"
#import "Sizes.h"
#import "UserModel.h"
#import "FamilyModel.h"
#import "RoomManagerSunVC.h"
#import "UIView+ZFFrame.h"
#import "FamilyRoomService.h"
#import "ZJScrollPageView.h"
@interface RoomManagerVC ()<UINavigationControllerDelegate,ZJScrollPageViewDelegate>

@property(weak, nonatomic)  ZJScrollPageView     *scrollPageView;
@property(strong, nonatomic)NSMutableArray  *titles;
@property(nonatomic,strong) FamilyRoomService *roomService;

@property(nonatomic,strong) NSMutableArray *roomsArray;

@property(nonatomic,assign) NSInteger seleIndex;
@end

@implementation RoomManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"房间管理"];
    self.seleIndex = 0;
    if ([UserTool getCurrentFamily].familyRole == 1) {
        [self addRightBtnImage:@"icon_home5"];
    }
}

-(NSMutableArray*)titles{
    if (_titles == nil) {
        _titles = [[NSMutableArray alloc] init];
    }
    return _titles;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getFamilyAllRooms];
}

-(void)getFamilyAllRooms{
    TYMWeak(self);
    [self.roomService getFindfamilyroomfamilyId:[UserTool getCurrentFamily].familyId completion:^(LMJBaseResponse * _Nonnull response, NSArray * _Nonnull roosArry) {
        if ([weakself.roomsArray count]) {
            [weakself.roomsArray removeAllObjects];
        }
        [weakself.roomsArray addObjectsFromArray:roosArry];
        if (weakself.roomsArray.count) {
            [weakself initScrollView];
        }
    }];
}

-(FamilyRoomService*)roomService{
    if (_roomService == nil) {
        _roomService = [[FamilyRoomService alloc] init];
    }
    return _roomService;
}

-(NSMutableArray*)roomsArray{
    if (_roomsArray == nil) {
        _roomsArray = [[NSMutableArray alloc] init];
    }
    return _roomsArray;
}

-(void)initScrollView{
    if (self.titles.count) {
        [self.titles removeAllObjects];
    }
    if (self.scrollPageView) {
        [self.scrollPageView removeFromSuperview];
        self.scrollPageView = nil;
    }
    
    for (RoomModel *model in self.roomsArray) {
        [self.titles addObject:model.roomName];
    }
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    style.titleFont = Font_Regular(16);
    style.selectedTitleFont = Font_Medium(16);
    style.showLine = YES;
    style.scrollLineHeight = 3;
    style.coverCornerRadius = 15;
    style.coverHeight = 30;
    style.scrollLineColor = [rgbaa(0,0,0,0) colorWithAlphaComponent:0];
    style.segmentHeight = 33;
    style.segmentViewBounces = NO;
    style.normalTitleColor = rgba(74, 74, 74);
    style.selectedTitleColor = rgba(74, 74, 74);
    style.contentViewBounces = NO;
    style.gradualChangeTitleColor = YES;
    style.adjustCoverOrLineWidth = YES;
    
    ZJScrollPageView *scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight - NavigateonHeight - SafeBottomMargin) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    
    [scrollPageView bringSubviewToFront:scrollPageView.segmentView];
    scrollPageView.contentView.frame = CGRectMake(0,style.segmentHeight, self.view.bounds.size.width, scrollPageView.zf_height-style.segmentHeight);
    UIView *colorLine = [[UIView alloc] initWithFrame:CGRectZero];
    colorLine.backgroundColor = rgba(53, 54, 70);
    [scrollPageView.segmentView.scrollLine addSubview:colorLine];
    [colorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(scrollPageView.segmentView.scrollLine);
        make.size.mas_equalTo(CGSizeMake(14, 3.5));
    }];
    scrollPageView.segmentView.backgroundColor = [UIColor whiteColor];
    scrollPageView.contentView.backgroundColor = [UIColor whiteColor];
    scrollPageView.contentView.collectionView.backgroundColor = [UIColor whiteColor];
    scrollPageView.backgroundColor = [UIColor whiteColor];
    if (self.seleIndex>self.titles.count-1) {
        self.seleIndex--;
    }
    [scrollPageView setSelectedIndex:self.seleIndex animated:NO];
    self.scrollPageView = scrollPageView;
    [self.view addSubview:self.scrollPageView];
}


-(void)rightFunc:(id)sender{
    AddRoomVC *addVC = [[AddRoomVC alloc] init];
    [self.navigationController pushViewController:addVC animated:YES];
}

//ZJScrollViewDelegate
- (NSInteger)numberOfChildViewControllers{
    return self.titles.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
    RoomManagerSunVC *childVC = (RoomManagerSunVC*)reuseViewController;
    if (childVC == nil) {
        childVC = [[RoomManagerSunVC alloc] initWithRoomModel:self.roomsArray[index]];
    }
    RoomModel *model = self.roomsArray.firstObject;
    childVC.pubRoomId =model.id;
    self.seleIndex = index;
    return childVC;
}

-(BOOL)shouldAutomaticallyForwardAppearanceMethods{
    return NO;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
